<?php
/**
 * Desc: Service模板
 * User: zdb
 * Date: 2023/6/5
 * Time: 11:22
 */

namespace app\api\service\project;
use think\facade\Db;

class ProjectBasicService
{

    private $primaryTable;

    public function __construct()
    {
        $this->primaryTable = "yj_project_basic";
    }

    public function findRowById($id)
    {
        return Db::table($this->primaryTable)->alias('a')
            ->leftJoin('yj_project_trunk pt', 'pt.id = a.project_trunk_id')
            ->where('a.id', '=', $id)
            ->field("a.*, pt.project_name")
            ->find();
    }

    public function findRowByData($data)
    {
        $condition = $this->assembleFilterCondition($data);
        return Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->strict(false)
            ->find();
    }

    public function getListByManyId($arrId)
    {
        $list = Db::table($this->primaryTable)->alias('a')
            ->whereIn('id', $arrId)
            ->select()
            ->toArray();
        return $list;
    }

    public function assembleFilterCondition($data)
    {
        if(isset($data['status'])){
            if(is_array($data['status'])){
                $condition = ['a.status '.$data['status'][0].$data['status'][1]];
            }else{
                $condition = ['a.status = '.$data['status']];
            }
        }else{
            $condition = array();
        }

        if (!empty($data['title'])) {
            $condition[] = "title like '%" . $data['title'] . "%'";
        }

        if (!empty($data['project_name'])) {
            $condition[] = "pt.project_name like '%" . $data['project_name'] . "%'";
        }

        if (!empty($data['example_id'])) {
            $condition[] = "example_id =".$data['example_id'];
        }

        if (!empty($data['is_show'])) {
            $condition[] = "is_show =".$data['is_show'];
        }

        return empty($condition) ? "" : implode(" AND ", $condition);
    }

    public function getListNoPage($data = [], $limitNum = 30)
    {
        $condition = $this->assembleFilterCondition($data);
        $res = Db::table($this->primaryTable)->alias('a')
            ->leftJoin('yj_project_trunk pt', 'pt.id = a.project_trunk_id')
            ->where($condition)
            ->limit(0, $limitNum)
            ->order('a.id desc')
            ->field("a.*, pt.project_name")
            ->select()
            ->toArray();
        return $res;
    }

    public function getListWithPage($data, $pageIndex = 1, $pageSize = 30)
    {
        $offset = ($pageIndex - 1) * $pageSize;
        $condition = $this->assembleFilterCondition($data);
        $res = Db::table($this->primaryTable)->alias('a')
            ->leftJoin('yj_project_trunk pt', 'pt.id = a.project_trunk_id')
            ->where($condition)
            ->limit($offset, $pageSize)
            ->order('a.id desc')
            ->field("a.*, pt.project_name")
            ->select();
        return $res;
    }

    public function getTotalNum($data)
    {
        $condition = $this->assembleFilterCondition($data);
        $num = Db::table($this->primaryTable)->alias('a')
            ->leftJoin('yj_project_trunk pt', 'pt.id = a.project_trunk_id')
            ->where($condition)
            ->count();
        return $num;
    }

    public function createRow($data)
    {
        $data['status'] = isset($data['status']) ? $data['status']:1;
        $res = Db::table($this->primaryTable)
            ->strict(false)
            ->insert($data, true);
        return $res;
    }

    public function createManyRow($data)
    {
        $res = Db::table($this->primaryTable)
            ->strict(false)
            ->duplicate($data)
            ->insertAll($data, true);
        return $res;
    }

    public function updateRowById($id, $data)
    {
        $res = Db::table($this->primaryTable)->alias('a')
            ->where('id', '=', $id)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function updateListByData($condition, $data)
    {
        $condition = $this->assembleFilterCondition($condition);
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function updateListByManyId($arrId, $data)
    {
        $res = Db::table($this->primaryTable)->alias('a')
            ->whereIn('a.id', $arrId)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeRowById($id)
    {
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)
            ->where('id', '=', $id)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeListByManyId($arrId)
    {
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)->alias('a')
            ->whereIn('a.id', $arrId)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeListByData($data)
    {
        $condition = $this->assembleFilterCondition($data);
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function addProject($data)
    {
        $serviceTrunk = new ProjectTrunkService();
        if($data['project_trunk_id']){
            //项目之前存在，现在升级版本
            $trunkInfo = $serviceTrunk->findRowById($data['project_trunk_id']);
            $addData = array(
                'project_trunk_id' => $trunkInfo['id'],
                'version' => intval($trunkInfo['version']) + 1
            );
            return $this->createRow($addData);
        }else{
            //项目之前不存在
            $addData = array(
                'project_name' => $data['project_name']
            );
            $newTrunkId = $serviceTrunk->createRow($addData);
            $addData = array(
                'project_trunk_id' => $newTrunkId,
                'version' => '1.0'
            );
            return $this->createRow($addData);
        }
    }

    /**
     * 如果项目名称有变化，则需要同步修改project trunk表
     * @param $data
     */
    public function editProject($data){
        $id = $data['id'];
        $info = $this->findRowById($id);
        $serviceProjectTrunk = new ProjectTrunkService();
        $trunkInfo = $serviceProjectTrunk->findRowById($info['project_trunk_id']);
        if($trunkInfo['project_name'] != $data['project_name']){
            $serviceProjectTrunk->updateRowById($trunkInfo['id'], array('project_name' => $data['project_name']));
        }
        return $this->updateRowById($id, $data);
    }
}